package pld.car.action;

import pld.car.bean.CustMsg;
import java.util.*;

public class CustMsgAction extends BaseAction {

	private CustMsg custMsg;

	private int rowCountPerPage;

	private int maxPageInBar;

	private String pageBar;

	private int pageNo;

	private List<Object> msgList;

	private String ids;

	private int msgID;

	private String returnMsg;

	public CustMsgAction() {
		System.out.println("CustMsgAction init!");
	}

	// 增加留言
	public String saveCustMsg() {
		try {
			this.getCustMsg().setCreateDate(new Date());
			this.getCustMsgService().insert(this.getCustMsg());
			this.setReturnMsg("留言保存成功。");
		} catch (Exception e) {
			this.setReturnMsg("留言保存失败。");
			e.printStackTrace();
		} finally {
			clearIt();
		}
		return "success";
	}

	// 操作完成之后，将相关变量的值清空
	private void clearIt() {
		this.setCustMsg(null);
	}

	// 初始化页面的时候需要初始化的一些设定值
	public void loadOptions() {
		// 获取每页显示的记录数
		String setValue = this.getSettingsService().getOption("Options", "rowCountPerPage");
		this.setRowCountPerPage(Integer.parseInt(setValue));
		// 分页工具条最多显示的页面数
		setValue = this.getSettingsService().getOption("Options", "maxPageInBar");
		this.setMaxPageInBar(Integer.parseInt(setValue));
	}

	// 按条件查询，得到新闻列表
	public String queryMsgList() {
		loadOptions();
		// 总共的记录数
		int count = this.getCustMsgService().getMsgCount();
		// 初始化分页工具栏的参数
		this.getPageService().initParams(count, this.pageNo, this.rowCountPerPage, this.maxPageInBar);
		// 生成分页工具栏
		this.setPageBar(this.getPageService().createPageBar());
		// 根据输入的sql查询当前页的记录
		this.setMsgList(this.getCustMsgService().getMsgTitle(this.getPageService().getFirstItemThisPage(), this.rowCountPerPage));
		return "success";
	}

	// 删除选中的记录
	public String deleteMsgs() {
		String ids[] = this.getIds().split(",");
		for (int i = 0; i < ids.length; i++) {
			int id = Integer.parseInt(ids[i]);
			// this.deleteNewsPics(id);
			this.getCustMsgService().delete(id);
		}
		return "success";
	}

	// 根据msgID查到对应的新闻记录
	public String findMsgByID() {
		this.setCustMsg(this.getCustMsgService().findMsgByID(this.getMsgID()));
		return "success";
	}

	public CustMsg getCustMsg() {
		return custMsg;
	}

	public void setCustMsg(CustMsg custMsg) {
		this.custMsg = custMsg;
	}

	public String getReturnMsg() {
		return returnMsg;
	}

	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}

	public int getRowCountPerPage() {
		return rowCountPerPage;
	}

	public void setRowCountPerPage(int rowCountPerPage) {
		this.rowCountPerPage = rowCountPerPage;
	}

	public int getMaxPageInBar() {
		return maxPageInBar;
	}

	public void setMaxPageInBar(int maxPageInBar) {
		this.maxPageInBar = maxPageInBar;
	}

	public String getPageBar() {
		return pageBar;
	}

	public void setPageBar(String pageBar) {
		this.pageBar = pageBar;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public List<Object> getMsgList() {
		return msgList;
	}

	public void setMsgList(List<Object> msgList) {
		this.msgList = msgList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getMsgID() {
		return msgID;
	}

	public void setMsgID(int msgID) {
		this.msgID = msgID;
	}

}
